package com.wedo.mall.activity.payment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.wedo.mall.R;
import com.wedo.mall.payment.Payment;
import com.wedo.mall.util.WUtils;
import com.wedo.mall.widget.PaymentView;


import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by e on 2017/7/31.
 */

public class PayActivity extends EActivity {

    public static final String EXTRA_ORDER = "order";
    public static final String EXTRA_PRICE = "price";

    @BindView(R.id.tv_order) TextView tv_order;
    @BindView(R.id.tv_total_price) TextView tv_total_price;
    @BindView(R.id.pv_payment) PaymentView pv_payment;

    @BindString(R.string.order_sn) String order_sn;
    @BindString(R.string.goods_price) String goods_price;

    private String order;
    private double totalPrice;

    private Payment payment;

    public static void toPay(Context context,String order,String price){
        Intent intent = new Intent(context,PayActivity.class);
        intent.putExtra(EXTRA_ORDER,order);
        intent.putExtra(EXTRA_PRICE, WUtils.toDouble(price));
        context.startActivity(intent);
    }

    @Override
    protected int layout() {
        return R.layout.activity_pay;
    }

    @Override
    protected void onActivityCreated(Bundle state) {

        order = getIntent().getStringExtra(EXTRA_ORDER);
        totalPrice = getIntent().getDoubleExtra(EXTRA_PRICE,0.00);

        tv_total_price.setText(String.format(goods_price,totalPrice));
        tv_order.setText(String.format(order_sn,order));
        payment = Payment.with(this).amount(String.valueOf(totalPrice)).listener(new Payment.OnPayListener() {
            @Override
            public void successful() {
                onPaySuccess();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        pv_payment.onResume();
    }

    @OnClick(R.id.btn_pay)
    protected void onPayClick(){
        if (pv_payment.iPay88()){
            payment.iPay88Order(order,totalPrice);
        } else if(pv_payment.paypal()) {
            payment.paypal();
        }else if(pv_payment.balance()) {
            if(!pv_payment.balanceEnough(totalPrice)) {
                toast(R.string.payment_no_enough);
            }else if (pv_payment.hasPassword()){
                payment.showPayPassword();
            }else {
                payment.showSetPassword(pv_payment);
            }
        }else {
            toast(R.string.payment_coming_soon);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        payment.onActivityResult(requestCode,resultCode,data);
    }

    private void onPaySuccess(){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ORDER,order);
        toActivity(PaySuccessActivity.class,bundle);
        defaultFinish();
    }
}
